package com.demo.apparel.service.web.Impl;

import com.alibaba.excel.EasyExcel;
import com.demo.apparel.entity.Ana;
import com.demo.apparel.mapper.web.AnaMapper;
import com.demo.apparel.service.web.AnaService;
import com.demo.apparel.vo.PageVO;
import com.demo.apparel.vo.web.AnaExportVO;
import com.demo.apparel.vo.web.AnaImportVO;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class AnaServiceImpl implements AnaService {

    @Autowired
    private AnaMapper anaMapper;

    @Override
    public List<Ana> getAll() {
        return anaMapper.selectAll();
    }

    @Override
    public void add(Ana ana) {
        ana.setId(UUID.randomUUID().toString().replace("-", ""));
        ana.setCreatedTime(LocalDateTime.now());
        ana.setUpdatedTime(LocalDateTime.now());
        anaMapper.insert(ana);
    }

    @Override
    public void delete(String id) {
        anaMapper.deleteById(id);
    }

    @Override
    public PageVO<Ana> getPage(String keyword, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<Ana> records = anaMapper.selectPage(keyword, offset, pageSize);
        long total = anaMapper.count(keyword);
        return new PageVO<>(records, total, pageNum, pageSize);
    }

    @Override
    @Transactional
    public void deleteBatch(List<String> ids) {
        if (ids != null && !ids.isEmpty()) {
            anaMapper.deleteBatch(ids);
        }
    }

    @Override
    public void exportAll(HttpServletResponse response) throws IOException {
        List<AnaExportVO> list = anaMapper.getAllForExport();

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        String fileName = URLEncoder.encode("语录导出", StandardCharsets.UTF_8);
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        EasyExcel.write(response.getOutputStream(), AnaExportVO.class)
                .sheet("语录数据")
                .doWrite(list);
    }

    @Override
    public void update(Ana ana) {
        ana.setUpdatedTime(LocalDateTime.now());
        anaMapper.updateById(ana);
    }

    @Override
    public void importAna(MultipartFile file) throws Exception {
        List<AnaImportVO> voList = EasyExcel.read(file.getInputStream(), AnaImportVO.class, null)
                .sheet()
                .doReadSync();

        List<Ana> anaList = voList.stream().map(vo -> {
            Ana ana = new Ana();
            ana.setId(UUID.randomUUID().toString().replace("-", ""));
            ana.setText(vo.getText());
            ana.setCreatedTime(LocalDateTime.now());
            ana.setUpdatedTime(LocalDateTime.now());
            return ana;
        }).collect(Collectors.toList());

        if (!anaList.isEmpty()) {
            anaMapper.insertBatch(anaList);
        }
    }


}
